package com.wj.rspmproperty.controller;

import com.wj.rspmproperty.pojo.PageResult;
import com.wj.rspmproperty.pojo.ResponsePojo;
import com.wj.rspmproperty.pojo.Room;
import com.wj.rspmproperty.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WeiJin
 * @version 1.0
 */
@Api(tags = "房间相关接口")
@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    /**
     * 根据楼层id查询房间
     *
     * @param floorId
     * @return
     */
    @GetMapping("/roomOfFloor/{floorId}")
    @ApiOperation(value = "根据楼层id查询房间", responseContainer = "List")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "floorId", type = "Integer", required = true)
    )
    public ResponsePojo<List<Room>> roomOfFloor(@PathVariable("floorId") Integer floorId) {
        List<Room> list = roomService.roomOfFloor(floorId);
        if (list.size() > 0) {
            return ResponsePojo.success(list);

        } else {
            return ResponsePojo.fail(list, "id查询不到房间");
        }
    }

    /**
     * 分页查询房间
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "分页查询所有房间信息，可以带关键字查询。",
            notes = "application/json格式<br/>" +
                    "{current:当前页 default 1 required false,<br/>" +
                    "size:每页数据量 default 10 required false,<br/>" +
                    "builtArea:建筑面积 required false <br/>" +
                    "status:房间状态 required false <br/>" +
                    "floorNumber: 房间所属楼层数 required false <br/>" +
                    "buildingNumber:房间所属建筑号 required false}")
    @PostMapping("/pageOfRoom")
    public ResponsePojo<PageResult<Room>> pageOfRoom(@RequestBody(required = false) Map<String, String> map) {
        map.putIfAbsent("current", "1");
        map.putIfAbsent("size", "10");
        return ResponsePojo.success(roomService.pageOfRoom(map));

    }

    /**
     * 批量修改房间状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PutMapping("/updateStatus")
    @ApiOperation(value = "批量修改房间状态", notes = "页面需要勾选多个房间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "需要修改状态的房间列表",
                    dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "status", required = true, value = "修改后的状态")
    })
    public ResponsePojo<Integer> changeRoomStatus(
            //使用字符串后续再处理
            @RequestParam("ids") String ids,
            @RequestParam("status") Integer status) {
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt).collect(Collectors.toList());
        if (list.size() > 0) {
            try {
                return ResponsePojo.success(roomService.changeRoomsStatus(list, status));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ResponsePojo.fail(null, "无数据");
    }
}
